package com.api.util.repo.impl;

import java.util.Map;

import com.api.services.IZyncroApi;
import com.api.test.Api;
import com.api.util.repo.DtoFactory;
import com.api.util.repo.DtoRepository;

public class DomainDtoFactory implements DtoFactory<Domain> {

	private static final String ZYNCRO_TEST_NAME = "ZYNCRO_TEST-";

	private final Api api;
	private final DtoRepository dtoRepository;
	private final String domainNameProperty;

	public DomainDtoFactory(Api api, DtoRepository dtoAccessor, String domainNameProperty) {
		this.api = api;
		this.dtoRepository = dtoAccessor;
		this.domainNameProperty = domainNameProperty;
	}

	@Override
	public Domain createDto() {

		String domainName = ZYNCRO_TEST_NAME + domainNameProperty;
		long domainQuota = 1073741824; // 1 GB
		int totalUsers = 2000;
		int domainType = 1;
		String userName = domainNameProperty;
		String lastName = "main_admin";
		String password = "123456";
		String email = domainNameProperty.toLowerCase() + "@yopmail.com";
		long userQuota = 10485760; // 10 MB
		int language = 0;

		Map<String, String> result = api.getService(IZyncroApi.class).createDomainNew(domainName, domainQuota, totalUsers, domainType,
				userName, lastName, password, email, userQuota, language);

		String mainAdminAppId = result.get("nameUser");

		Domain domain = new Domain();
		domain.setDomainUrn(result.get("domainUrn"));
		domain.setMainAdmin(mainAdminAppId);
		domain.setDomainQuota(domainQuota);
		domain.setDomainName(domainName);
		domain.setTotalUsers(totalUsers);
		domain.setLanguage(language);

		final DomainUser mainAdmin = new DomainUser(email, password, mainAdminAppId);
		mainAdmin.setQuota(userQuota);
		mainAdmin.setName(userName);
		mainAdmin.setLastName(lastName);

		dtoRepository.saveDto(domainNameProperty, mainAdmin);

		return domain;
	}
}
